<?php
namespace app\index\controller;

use think\Loader;
use think\Request;
use app\common\model\Customer as CustomerModel;
use app\common\model\Admin as AdminModel;

/**
 * 网站首页 首页算是一个单独的模块 
 * Class Index
 * @package app\index\controller
 */
class Index extends Base{

    private function index2(){

        return view($this->template_root.'index.html',$this->data);
    }

    public function test(Request $request){
        $code = $request->param("invite");
        /*if(!$code){
            return view('404.html',$this->data);
        }*/
        $count = AdminModel::where(['invitecode'=>$code])->count();
        /*if(!$count){
            return view('404.html',$this->data);
        }*/
        $this->data['code'] = $code;
        return view('',$this->data);
    }

    public function index(Request $request){
        $code = $request->param("invite");
        /*if(!$code){
            return view('404.html',$this->data);
        }*/
        $count = AdminModel::where(['invitecode'=>$code])->count();
        /*if(!$count){
            return view('404.html',$this->data);
        }*/
        $this->data['code'] = $code;
        return view('',$this->data);
    }

    public function enroll(Request $request){
        $data = $request->param();

        $validate = Loader::validate("customer");

        $res = $validate->check($data);
        if(!$res){
            $this->error($validate->getError());
        }
        $count = AdminModel::where(['invitecode'=>$data['invite_id']])->count();
        if(!$count){
            $this->error('请重新扫码进入');
        }
        $data['add_time'] = time();
        $customerModel = new CustomerModel();
        $res = $customerModel->allowField(true)->create($data);
        if($res){
            $this->success("报名成功");
        }
        $this->error("报名失败");
    }

}